using Game.Main.Model;
using UnityEngine;

namespace Game.Component
{
	public class StethoscopeBoxItem : BaseItem
	{
		private enum State
		{
			None = 0,
			Fold = 1,
			Unfold = 2
		}

		private State state;

		private StethoscopeItem stethoscope;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
		}

		public override void ResetState()
		{
			base.ResetState();
			CheckState();
		}

		private void CheckState()
		{
			if (parentItem != null && typeof(CharacterItem).IsAssignableFrom(parentItem.GetType()))
			{
				SetState(State.Unfold);
			}
			else
			{
				SetState(State.Fold);
			}
		}

		public override void TouchDown(TouchEventData touchEventData)
		{
			base.TouchDown(touchEventData);
			SetState(State.Unfold);
		}

		public override void TouchUp(TouchEventData touchEventData)
		{
			base.TouchUp(touchEventData);
			CheckState();
		}

		public override bool AttachItem(BaseItem item, DepthType depthType, AttachType attachType, bool updatePosition)
		{
			bool flag = base.AttachItem(item, depthType, attachType, updatePosition);
			if (flag)
			{
				if (state == State.Fold)
				{
					item.SetVisible(false);
				}
				if (item.itemData.scriptType == 119)
				{
					stethoscope = (StethoscopeItem)item;
				}
			}
			return flag;
		}

		private void SetState(State val)
		{
			if (state == val)
			{
				return;
			}
			state = val;
			Transform transform = model.Find("Fold");
			Transform transform2 = model.Find("Unfold");
			if (transform == null || transform2 == null)
			{
				return;
			}
			transform.gameObject.SetActive(state == State.Fold);
			transform2.gameObject.SetActive(state == State.Unfold);
			if (state == State.Fold)
			{
				SetChildsVisible(false);
				if (stethoscope != null && !ContainsChild(stethoscope))
				{
					stethoscope.PutBackPlaceItemImmediately();
					stethoscope.SetVisible(false);
				}
				coll.offset = transform.localPosition;
				coll.size = transform.GetComponent<BoxCollider2D>().size;
			}
			else
			{
				if (state != State.Unfold)
				{
					return;
				}
				SetChildsVisible(true);
				coll.offset = transform2.localPosition;
				coll.size = transform2.GetComponent<BoxCollider2D>().size;
				for (int i = 0; i < childItems.Count; i++)
				{
					if (childItems[i] != null && childItems[i].itemData.scriptType == 95)
					{
						((RopeItem)childItems[i]).ResetJoint();
						break;
					}
				}
			}
		}
	}
}
